// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    today: "",
    functionData: [
      '../../images/ai.png',
      '../../images/zp.png',
      '../../images/ai2.png',
      '../../images/sc.png',
    ],
    ainame: '',
    mNEData: [{
      title: '早餐',
      one: '../../images/red.png',
      two: '../../images/red.png',
      star: 2.5,
      isFinish: false,
    }, {
      title: '午餐',
      one: '../../images/red.png',
      two: '../../images/red.png',
      star: 3,
      isFinish: false
    }, {
      title: '晚餐',
      one: '../../images/red.png',
      two: '../../images/red.png',
      star: 2,
      isFinish: false
    }, ]
  },
  dk: function (e) {
    console.log(e.currentTarget.dataset.i);
    var i = e.currentTarget.dataset.i
    var d = this.data.mNEData
    d[i].isFinish = true
    this.setData({
      mNEData: d
    })
  },
  functionTap: function (e) {
    console.log(e.currentTarget.dataset.i);
    var i = e.currentTarget.dataset.i
    if (i == 0) {
      this.AI_recognition_click()
    } else if (i == 1) {
      wx.navigateTo({
        url: '../zhuanpan/zhuanpan',
      })
    } else if (i == 2) {
      //问答机器人

    } else if (i == 3) {
      wx.navigateTo({
        url: '../collection/collection',
      })
    }
  },
  goToSearch: function () {
    wx.navigateTo({
      url: '../sousuo/sousuo',
    })
  },
  dispath: function (e) {
    //e.currentTarget.dataset.text获取点击的模块的值
    console.log(e);
    wx.navigateTo({
      url: '../dishDetails/dishDetails?info=' + encodeURIComponent(JSON.stringify(e.currentTarget.dataset.index)),
    })
    console.log(e.currentTarget.dataset.index);
  },
  //调取相机和相册
  AI_recognition_click: function () {
    var FSM = wx.getFileSystemManager();
    var ainame;
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original'],
      sourceType: ['album', 'camera'],
      success(res) {
        wx.showLoading({
          title: '正在识别...',
        })
        const tempFilePaths = res.tempFilePaths
        for (let r in res.tempFilePaths) {
          console.log(res.tempFilePaths[r]);
          FSM.readFile({
            filePath: res.tempFilePaths[r],
            encoding: "base64",
            success: function (data) {
              wx.request({
                url: 'https://www.daizhongdai.xyz:3007/getApple',
                method: 'POST',
                data: {
                  image: data.data
                },
                success: (res) => {

                  console.log(res);
                  var n = res.data.result[0].name
                  var r = res.data.result
                  console.log("cainame" + n);
                  if (n == "非果蔬食材") {
                    
              console.log(data.data);
                    wx.request({
                      url: 'https://www.daizhongdai.xyz:3007/getDish',
                      method: 'POST',
                      headers:{
                        "content-type": "application/x-www-form-urlencoded"
                      },
                      data: {
                        image: data.data
                      },
                      success:res=>{
                        console.log(res);
                         n = res.data.result[0].name
                         r = res.data.result
                         wx.navigateTo({
                          url: '../nutrition/nutrition?name=' + JSON.stringify(r)
                        })
                      } , fail: (err) => {
                        console.log(err);
                        wx.showToast({
                          title: '识别失败，请检查网络！',
                        })
                      },complete:()=>{
                        
                        wx.hideLoading({
                          success: (res) => {},
                        })
                      }
                    })
                    return
                  }
                  wx.hideLoading({
                    success: (res) => {},
                  })
                  wx.navigateTo({
                    url: '../nutrition/nutrition?name=' + JSON.stringify(r)
                  })
                },
                fail: () => {
                  wx.hideLoading({
                    success: (res) => {},
                  })
                  wx.showToast({
                    title: '识别失败，请检查网络！',
                  })
                }
              })
            }
          })
        }
      }
    })

    //     wx.showActionSheet({
    //       itemList: ['拍照', '从相册中选择'],
    //       success(res) {
    // console.log(res.tapIndex)
    // let sourceType = 'camera'
    // if (res.tapIndex == 0) {
    //   sourceType = 'camera'
    // } else if (res.tapIndex == 1) {
    //   sourceType = 'album'
    // }

    //  },
    //     })

  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  add0: function (d) {
    return d < 10 ? '0' + d : d
  },

  onLoad() {
    var d = new Date()
    this.setData({
      today: d.getFullYear() + "年" + this.add0(d.getMonth() + 1) + "月" + this.add0(d.getDate()) + "日"
    })
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    // wx.login({
    //   success: res => {
    //     // 发送 res.code 到后台换取 openId, sessionKey, unionId
    //     console.log("this is code" + JSON.stringify(res));
    //     wx.request({
    //       url: 'https://www.daizhongdai.xyz:3007/login',
    //       data: {
    //         code: res.code
    //       },
    //       header: {
    //         'content-type': 'application/json'
    //       },
    //       success(res) {
    //         console.log(res.data.token);
    //         wx.setStorageSync('token', res.data.token)
    //         var user = wx.getStorageSync('user')
    //         console.log(user);

    //       }
    //     })
    //   }
    // })
    // wx.request({
    //   url: 'https://www.daizhongdai.xyz:3007/index/tj',
    //   success: res => {
    //     console.log(res.data.data.data);
    //     this.setData({
    //       dataList: res.data.data.data
    //     })
    //   }
    // })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})